package tech.treec.smartlight.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import tech.treec.smartlight.R;
import tech.treec.smartlight.callback.ButtonClickCallback;

/**
 * Created by Young on 2025/1/20.
 */
public class MyAdapter extends BaseAdapter {
    private static final String TAG = "MyAdapter";
    private Context mContext;
    private List<Map<String, String>> mList;
    private ButtonClickCallback callback;
    private int mViewId;

    public MyAdapter(Context context, int viewId, List<Map<String, String>> list, ButtonClickCallback callback) {
        this.mContext = context;
        this.mList = list;
        this.mViewId = viewId;
        this.callback = callback;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(mViewId, null);
//            viewHolder.nameTX = convertView.findViewById(R.id.name);
//            viewHolder.imageV = convertView.findViewById(R.id.img);
            viewHolder.btn = convertView.findViewById(R.id.btn_switch);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.btn.setText(mList.get(position).get("name"));
        viewHolder.btn.setBackgroundColor(R.color.org);
        viewHolder.btn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24);
        viewHolder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, mList.get(position).get("name"));
                callback.onClick(mList.get(position).get("name"),position);
            }
        });
        return convertView;
    }

    class ViewHolder {

        private Button btn;
    }

}